package engine.gui.elements;

import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.Disk;

import engine.gui.Action;
import engine.gui.Component;
import engine.gui.GUIStyle;
import engine.util.ortho.Draw2D;
import engine.util.vector.Color;

public class Slider extends Component{
	private static final int SLIDER_HEIGHT = 6;
	
	private double currentValue;
	private double maxValue;
	private double minValue;
	
	private Disk disk;
	
	private boolean onDrag;
	
	private Action action;

	public Slider() {
		disk = new Disk();
	}
	
	public Slider(int posX, int posY, int width, int height) {
		this();
		setPosX(posX);
		setPosY(posY);
		setWidth(width);
		setHeight(height);
	}
	
	@Override
	public void render(int x, int y, int width, int height) {
		GUIStyle style = getCurrentStyle();
		
		Color.bindColor4f(style.getSliderBackgroundColor());
		
		if(style.getSliderBackgroundImage() != null) {
			style.getSliderBackgroundImage().draw(x + getPosX(), y + getPosY(), getWidth(), getHeight());
		} else {
			Draw2D.fillRect(x + getPosX(), y + getPosY() + (getHeight() - SLIDER_HEIGHT) / 2, getWidth(), SLIDER_HEIGHT);
		}
		
		Color.bindColor4f(style.getSliderSelectorColor());
		
		if(style.getSliderSelectorImage() != null) {
			style.getSliderSelectorImage().draw(x + getPosX() + (int)(getPercentage() * (getWidth() - getHeight())), y + getPosY(), getHeight(), getHeight());
		} else {
			GL11.glPushMatrix();
			GL11.glTranslated(x + getPosX() + getHeight() / 2 + getPercentage() * (getWidth() - getHeight()), y + getPosY() + getHeight() / 2 , 0);
			disk.draw(0, getHeight() / 2, 10, 10);
			GL11.glPopMatrix();
		}
	}
	
	public double getPercentage() {
		return currentValue / (maxValue - minValue);
	}
	
	@Override
	public boolean mousePressed(int posX, int posY, int button) {
		if(button == 0 && posX > getPosX() + getPercentage() * (getWidth() - getHeight()) && posX < getPosX() + getPercentage() * (getWidth() - getHeight()) + getHeight() && posY > getPosY() && posY < getPosY() + getHeight()) {
			onDrag = true;
			return true;
		}
		
		return false;
	}
	
	@Override
	public boolean mouseDragged(int posX, int posY, int button) {
		if(onDrag) {
			currentValue = ((posX - (getPosX() + getHeight() / 2)) / (float)(getWidth() - getHeight())) * (maxValue - minValue) ;
			
			currentValue = Math.max(minValue, Math.min(maxValue, currentValue));
			
			if(action != null) {
				action.performAction();
			}
		}
		return false;
	}
	
	@Override
	public boolean mouseReleased(int posX, int posY, int button) {
		onDrag = false;
		return false;
	}

	@Override
	public void reset() {
		//do nothing
	}

	public double getCurrentValue() {
		return currentValue;
	}

	public void setCurrentValue(double currentValue) {
		this.currentValue = currentValue;
	}

	public double getMaxValue() {
		return maxValue;
	}

	public void setMaxValue(double maxValue) {
		this.maxValue = maxValue;
	}

	public double getMinValue() {
		return minValue;
	}

	public void setMinValue(double minValue) {
		this.minValue = minValue;
	}

	public Action getAction() {
		return action;
	}

	public void setAction(Action action) {
		this.action = action;
	}

}
